package sunyu.kit.database;

import org.nutz.dao.impl.NutDao;
import org.slf4j.Logger;
import sunyu.kit.common.ExceptionKit;
import sunyu.kit.common.LogKit;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

/**
 * 扩展NutzDao，用于避免多次创建相同的数据连接
 *
 * @author 孙宇
 */
public class NutzDao extends NutDao {

    private static final Logger logger = LogKit.getLogger();

    public NutzDao(DataSource dataSource) {
        super();
        super.setDataSource(dataSource);
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public Connection getConnection() {
        try {
            return getDataSource().getConnection();
        } catch (SQLException e) {
            logger.error(ExceptionKit.getStackTrace(e));
        }
        return null;
    }

    public DatabaseMetaData getMetaData() {
        try {
            return getConnection().getMetaData();
        } catch (SQLException e) {
            logger.error(ExceptionKit.getStackTrace(e));
        }
        return null;
    }

}
